<?php
include('../../__global.php');
include(SERVER_ROOT_PATH.'/include/config/dataRule/ml_datarule_content.php');
class aj_comment extends ml_controller 
{
    private $_rid;
    private $_uid;
    private $_content;
    private $_content_rid;
    private $_com_id;
    private $oComment;
    private $oldComment;
    private $_cmt_id;
    protected $mid_uid;
    
    public function initParam()
    {
        $this->_rid=$this->input('rid','p');
        $this->_uid=ml_tool_resid::resid2uid($this->_rid);
        $this->mid_uid=$this->input('uid','p');
        $this->_content=$this->input('content','p');
        $this->_content=preg_replace('/\s+/',' ', $this->_content); 
        $this->_com_id =$this->input('comid','p','0');
        $this->_content_rid=$this->input('gid','p');
        $this->_commentme = $this->input('commentme','p',0);
        $this->oComment=new ml_model_dbComment();
        
        if(ml_tool_resid::resid2type($this->_content_rid) != ML_RESID_TYPE_CONTENT){
            $this->_content_rid = 0;
        }
        if(empty($this->_content_rid) && ml_tool_resid::resid2type($this->_rid)==ML_RESID_TYPE_CONTENT){
            $this->_content_rid = $this->_rid;
        }
        
    }
    public function checkParam()
    {
        $this->check_referer();
        if(!$this->check_permission(ML_PERMISSION_LOGIN_ONLY)) {
            $this->api_output(ML_RCODE_NOLOGIN);
        }
        else if(!$this->check_permission(ML_PERMISSION_LOGIN_CANWRITE)) {
            $this->api_output(ML_RCODE_NOACTIVE);
        }
        if(empty($this->_rid)){
            $this->api_output(ML_RCODE_PARAM);
        }
        if(!ml_tool_resid::is_resid($this->_rid)){
            $this->api_output(ML_RCODE_PARAM);
        }
        if(!ml_tool_isuid::is_mluid($this->_uid)){
            $this->api_output(ML_RCODE_PARAM);
        }

        if( !empty($this->mid_uid) && $this->_uid == $this->mid_uid ){
            $this->mid_uid='';
        }

        if(empty($this->_content)) 
            $this->api_output(ML_RCODE_PARAM);
        if(Tool_string::str_width($this->_content) > ML_DATARULE_COMMENT_MAXLEN )
            $this->api_output(ML_RCODE_PARAM);
        if(ml_tool_keyword::haveSensitiveWord($this->_content))
            $this->api_output(ML_RCODE_PARAM);
        
        if(!empty($this->_com_id)){
        
            $reply_uid=$this->_uid;
            if($this->_commentme){
                $reply_uid=$this->__visitor['uid'];
            }
        
            $this->oComment->getCommentById($reply_uid,$this->_com_id);
            $this->oldComment=$this->oComment->get_data();

            $nickArr=ml_tool_nick::uids2nicks(array(0=>$this->oldComment['act_uid']));
            
            $reg='/@([^\s:]+)\s?:/';
            @preg_match($reg,$this->_content,$match);
            if($match[1]!= $nickArr[$this->oldComment['act_uid']]){
                $this->api_output(ML_RCODE_PARAM);
            }
            ml_tool_sendMq::add_atnick_latestAt($this->__visitor['uid'], $this->oldComment['act_uid']);
        }
        //用于喜欢的pinboard和likeme页中feed 进行的评论 mid_uid为喜欢的中间人
        if(ml_tool_resid::resid2type($this->_rid)==ML_RESID_TYPE_CONTENT  && !empty($this->mid_uid)  ){
            $ofeed = new ml_model_dbUserfeed();
            $rs=$ofeed->getFeedByAtt($this->mid_uid,$this->_rid);
            if(!$rs){
                $this->api_output(ML_RCODE_PARAM);
            }
            $feeds=$ofeed->get_data();
            $this->_rid = $feeds[0]['rid'];
            $this->_uid=$this->mid_uid;        //形成feed下评论条件
            //var_dump($this->_rid);var_dump($this->_uid);die();
            
        }
        
    }
    
    public function incrComCount($rid){
        
        if(!isset($oRedis)){
            $oRedis=new ml_model_rdsCount();
        }
        $rs = $oRedis->incrCommentCount($rid);
        return $rs ? true:false;
    } 
    public function getComCountNum($rid){
        if(!isset($oRedis)){
            $oRedis=new ml_model_rdsCount();
        }
        $aRid=array(
            0 =>$rid    
        );
        $rs = $oRedis->getCommentCount($aRid);
        if(!$rs)  return false;
        $count=$oRedis->get_data();
        return $count[$rid];
    }
    
    public function getUids(){
        $aNicks = ml_tool_nick::findAllAtNick($this->_content);
        $aAtUids=array();
        if(count($aNicks) > 0 )
            $aAtUids = ml_tool_nick::nicks2uids($aNicks);
        
        $uidArr[]=$this->_uid;
        if(!empty($this->_com_id)){
            $uidArr[]=$this->oldComment['act_uid'];      //回复不进@
        }
        return array_diff($aAtUids,$uidArr);    
        
        /*
        $pos = array_search($this->_uid , $aAtUids);
        if($pos !== false)
            unset($aAtUids[$pos]);
        return     $aAtUids;
        */
        
    }
    
    
    public function main()
    {
        $oRds = new ml_model_rdsNotice();

        $data=array(
            'rid'=>$this->_rid,
            'uid'=>$this->_uid,
            'act_uid'=>$this->__visitor['uid'],
            'content'=>$this->_content,
            'content_rid'=>$this->_content_rid,
            'reply_id'=>$this->_com_id
        );
        
        $rs=$this->oComment->insertComment($data);
        if(!$rs)
            $this->api_output(ML_RCODE_BUSY);
        
        $oAdmin = new ml_model_admin_dbCommon();
        $oAdmin->comment_add($this->__visitor['uid'] , $this->_content , $this->_content_rid);


        $this->incrComCount($this->_rid);
        $this->_cmt_id=$this->oComment->insert_id();   //最后插入的评论id
        $col_comment=$data;
        $col_comment['com_id']=$this->_cmt_id;
        ml_tool_sendMq::collect_comment($col_comment);  //为后台收集评论
        
        $aAtUids=$this->getUids();
        
        $totalNum= $this->getComCountNum($this->_rid);
        $back=array(
            'comid'=> $this->_cmt_id,
            'totalNum'=> $totalNum ? $totalNum : 0 
            );
        
        if($this->_uid != $this->__visitor['uid'])    
            $oRds->add_notice($this->_uid , ml_model_rdsNotice::NTCTYPE_ADDCOMMENT);
            
        //回复不进@了，评论给谁须通知收到
        if(!empty($this->_com_id)){
            if($this->oldComment['act_uid'] != $this->_uid ){      //宝贝的主人不必收到
                $data['uid']=$this->oldComment['act_uid'];
                $rs=$this->oComment->insertComment($data);
                if($data['uid']!= $this->__visitor['uid'])          //被回复的人不是当前访问者才发通知
                    $oRds->add_notice($data['uid'] , ml_model_rdsNotice::NTCTYPE_ADDCOMMENT);
            }
        }
        
        //如果非feed、非转发喜欢pin 下直接进行评论    ，不双发
        if(ml_tool_resid::resid2type($this->_rid)==ML_RESID_TYPE_CONTENT){
            if(!empty($this->_com_id)){
                ml_tool_sendMq::atme_in_replycomment($this->__visitor['uid'],$this->_rid,$this->_cmt_id,$this->oldComment['act_uid'],$data['reply_id'],$aAtUids);
            }else{
                ml_tool_sendMq::atme_in_newcomment($this->__visitor['uid'],$this->_rid,$this->_cmt_id,$aAtUids);
            }
            
            ml_tool_sendMq::userAction2GoodsRank($this->_rid , 'cmt');
            //行为LOG
            ml_tool_actionlog::add_cmt($this->__visitor['uid'] , $this->_rid , $this->_content , 0 , count($aAtUids));
            $this->api_output(ML_RCODE_SUCC,$back);
        }
        
        //双发，在feed的评论中写入一条，在宝贝中再次写入一条，内容一致，实现feed主人与宝贝主人的双发
        if(!empty($this->_content_rid)){
            
            $data['rid'] = $this->_content_rid;
            $feed_uid=$data['uid'];
            $content_uid = ml_tool_resid::resid2uid($data['rid']);
            $data['uid']=$content_uid;
            
            //因双发产生不同的com_id,须在此获取另一边回复评论的id，后期可进行改造，须动表，增加com_rid,双发评论com_rid一致  @todo 
            if(!empty($this->_com_id)){
                $this->oComment->getCommentById($feed_uid,$this->_com_id);
                $replyCon=$this->oComment->get_data();
                
                $where['act_uid']= $replyCon['act_uid'];
                $where['content_rid'] = $replyCon['content_rid'];
                $where['ctime'] = $replyCon['ctime'];
                
                $this->oComment->getComByCondition($where,$content_uid);
                $conReply= $this->oComment->get_data();
                $data['reply_id']=$conReply[0]['com_id']; 
            }
            
            $rs=$this->oComment->insertComment($data);
            if(!$rs)
                $this->api_output(ML_RCODE_BUSY);
            
            
            $this->incrComCount($data['rid']);
            $this->_conmt_id=$this->oComment->insert_id(); 
            
            if($content_uid == $feed_uid){
                $this->oComment->updateUid($content_uid,$this->_conmt_id);  //@todo 以后队列
            }else{
                $aAtUids=$this->getUids();
                if(!empty($this->_com_id)){
                    ml_tool_sendMq::atme_in_replycomment($this->__visitor['uid'],$data['rid'],$this->_conmt_id,$this->oldComment['act_uid'],$data['reply_id'],$aAtUids);
                }else{
                    ml_tool_sendMq::atme_in_newcomment($this->__visitor['uid'],$data['rid'],$this->_conmt_id,$aAtUids);    
                }
                
                if($content_uid != $this->__visitor['uid'])
                    $oRds->add_notice($content_uid , ml_model_rdsNotice::NTCTYPE_ADDCOMMENT);
            }
        }
        $this->api_output(ML_RCODE_SUCC,$back);
    }
    
}

new aj_comment();